package cn.lili.controller.system;


import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.json.JSONUtil;
import cn.lili.common.enums.ResultCode;
import cn.lili.common.exception.ServiceException;
import cn.lili.common.utils.ResultUtil;
import cn.lili.common.vo.ResultMessage;
import cn.lili.modules.system.constants.DictConstant;
import cn.lili.modules.system.entity.dos.Dictionary;
import cn.lili.modules.system.entity.dos.Setting;
import cn.lili.modules.system.entity.dto.ImSetting;
import cn.lili.modules.system.entity.enums.SettingEnum;
import cn.lili.modules.system.service.DictionaryService;
import cn.lili.modules.system.service.SettingService;
import com.alibaba.fastjson.JSONObject;
import com.service.IMService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * IM控制器
 *
 * @author Chopper
 * @version v1.0
 * 2021-09-16 15:32
 */
@RestController
@RequestMapping("/system/im")
@Api(tags = "IM 中心")
@Slf4j
public class IMController {

    @Autowired
    private  SettingService settingService;
    @Autowired
    private  DictionaryService dictionaryService;
    @Autowired
    private  IMService imService;

    @ApiOperation(value = "获取IM接口前缀")
    @GetMapping
    public ResultMessage<String> getUrl() {
        String imUrl;
        try {
            Setting imSettingVal = settingService.get(SettingEnum.IM_SETTING.name());
            ImSetting imSetting = JSONUtil.toBean(imSettingVal.getSettingValue(), ImSetting.class);
            imUrl = imSetting.getHttpUrl();
        } catch (Exception e) {
            throw new ServiceException(ResultCode.PLATFORM_NOT_SUPPORTED_IM);
        }
        return ResultUtil.data(imUrl);
    }

    /*
     * 双向撤回
     */
    @GetMapping("/clear")
    public ResultMessage<Object> clear(String fromId, String targetId, Integer type) {
        log.info("begin ===");
        List<Dictionary> dictionaries = dictionaryService.dictionryListByCode(DictConstant.CLEAR_ID_NAME, null);
        if (CollectionUtil.isNotEmpty(dictionaries)) {
            for (Dictionary dictionary : dictionaries) {
                log.info("begin dictionaries=== {}", JSONObject.toJSONString(dictionary));
                String[] idList = dictionary.getDescription().split(",");
                for (String string : idList) {
                    //只有与字典匹配上的人，才去双向撤回。
                    if (string.equals(fromId) || string.equals(targetId)) {
                        log.info("1  string == {}, fromId == {}, targetId == {}, type == {}", string, fromId, targetId, type);
                        imService.clearTypeMessage(fromId, targetId, type);
                    }
                }
            }
        }
        return ResultUtil.success();
    }


}
